﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Manage_College : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btn_Save_Click(object sender, EventArgs e)
    {
        if (fu_CollegeLogo.HasFile) SaveFile();

        eSYSU.Model.College college = new eSYSU.Model.College();
        college.name = tb_CollegeName.Text;
        college.brief = tb_CollegeBrief.Text;
        college.order = tb_CollegeOrder.Text == String.Empty ? 0 : Convert.ToInt32(tb_CollegeOrder.Text);
        college.logo = fu_CollegeLogo.FileName;

        eSYSU.Business.CollegeService.CollegeInsert(college);
        Response.Write("Success");
    }

    private bool SaveFile()
    {
        string path = Server.MapPath("~/Uploads/logos/");
        string fileExtension = System.IO.Path.GetExtension(fu_CollegeLogo.FileName).ToLower();
        string allowedExtensions = ".gif|.png|.bmp|.jpg";
        string fullPath = path + fu_CollegeLogo.FileName;
        if (allowedExtensions.IndexOf(fileExtension) > -1)
        {
            try
            {
                fu_CollegeLogo.SaveAs(fullPath);
                int insertIndex = fullPath.LastIndexOf('.');
                string[] sDePath = new string[] { fullPath.Insert(insertIndex, "_t200"), 
                                                      fullPath.Insert(insertIndex, "_t120"),
                                                      fullPath.Insert(insertIndex, "_t60") };
                int[,] iSize = new int[3, 2] { { 200, 200 }, { 120, 120 }, { 60, 60 } };   //缩略图尺寸
                //创建缩略图
                if (eSYSU.Business.CollegeService.CreateLogoThumbnail(fullPath, sDePath, iSize))
                    return true;
                else return false;
            }
            catch
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}